﻿// Copyright (c) 2012 - 2021 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2021-03-19 version 4.0.0 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "exception.h"
#include "ServerLoader.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// _tmain
#if (__PLATFORM_TARGET__ == PLATFORM_TARGET_WINDOWS)
BOOL WINAPI ConsoleHandler(DWORD)
{
    GServerLoaderInst->Stop();
    CPlatform::SleepEx(100);
    return FALSE;
}
#else
void SignalHandler(Int)
{
    GServerLoaderInst->Stop();
    CPlatform::SleepEx(100);
}
#define _tmain   main
#endif

Int _tmain(Int argc, XChar* argv[])
{
#if (__PLATFORM_TARGET__ == PLATFORM_TARGET_WINDOWS)
    CSEHExceptionFilter::Init();

    ::SetConsoleCtrlHandler(ConsoleHandler, TRUE);
    CPlatform::SetExceptHandler(&CServerLoader::ThreadExceptHandler);
#else
    signal(SIGPIPE, SIG_IGN);
    signal(SIGHUP,  SignalHandler);
    signal(SIGKILL, SignalHandler);
    signal(SIGTERM, SignalHandler);

    CExceptionTerminater::Init();
#endif
    if (GServerLoader::NewInstance() == nullptr)
    {
        DEV_ERROR(TF("GServerLoader Singleton create failed!"));
        return -1;
    }

    CoreInit();
    if (GServerLoaderInst->Init(argc, argv))
    {
        GServerLoaderInst->Start();
    }
    GServerLoaderInst->Exit();
    CoreExit();

    GServerLoader::DelInstance();

#if (__PLATFORM_TARGET__ == PLATFORM_TARGET_WINDOWS)
    CSEHExceptionFilter::Exit();
#else
    CExceptionTerminater::Exit();
#endif
    return 0;
}




